package com.xx.bysj.security;


import com.xx.bysj.dto.HnUser;
import com.xx.bysj.exception.BusinessException;
import com.xx.bysj.mapper.HnUserMapper;
import com.xx.bysj.service.UserService;
import com.xx.bysj.utils.NumberUtils;
import com.xx.bysj.utils.SecurityUtils;
import com.xx.bysj.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Autowired
    private HnUserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        HnUser dto = new HnUser();
        dto.setUserName(username);
        List<HnUser> userList = userMapper.selectByUserNameList(dto);
        if (userList.size() == NumberUtils.ZERO) {
            throw new BusinessException("该用户不存在");
        }
        UserHolder.saveUser(userList.get(0));
        return new SecurityUtils(userList.get(0));
    }


}
